package crazy_eights_test;
import crazy_eights.Card;

import java.util.Vector;


public class CardTest {

    static private String TEST_NAME = "Card Test";
    static private String PASSED_STR = "Passed: ";
    
    static private String COMPARISON_TEST_STR = "Comparison Test";
    
    private Vector<Card> cardList;
    
    CardTest()
    {
        cardList = new Vector<Card>();
        // Create the array of cards
        SetupOrderedCards(cardList);
    }
    
    public Boolean RunTest() throws Exception
    {
        System.out.print("Executing: ");
        System.out.print(TEST_NAME);
        System.out.println("");
        try
        {
        if(ComparisonTest())
        {
            System.out.println(PASSED_STR + COMPARISON_TEST_STR);
        }
        }
        catch(Exception e)
        {
            String msg = "CardTest:RunTest->" + e.getMessage();
            e = new Exception(msg);
            throw e;
        }
        return true;
    }
    
    private Boolean ComparisonTest()throws Exception
    {
        try
        {
        if(CheckOrdered())
        {
            System.out.println("Passed: Check Ordered");
        }
        }
        catch(Exception e)
        {
            String msg = "ComparisonTest->" + e.getMessage();
            e = new Exception(msg);
            throw e;
        }
        return true;
    }

    private Boolean CheckOrdered() throws Exception
    {
        String funcStr = "CheckOrdered";
        for(int i = 0; i < cardList.size(); i++)
        {
            Card left = cardList.get(i);
            for(int j = 0; j < cardList.size(); j++)
            {
                Card right = cardList.get(j);
                if(i==j)
                {
                    // Card should be less than the same card
                    if(left.lessThan(right))
                    {
                        throw new Exception(funcStr + ": " + left.toString() + " < " + right.toString());
                    }
                }
                else if(i < j)
                {
                    if( !left.lessThan(right))
                    {
                        throw new Exception(funcStr + ": " + left.toString() + " > " + right.toString());
                    }
                }
                else
                {
                    if(left.lessThan(right))
                    {
                        throw new Exception(funcStr + ": " + left.toString() + " < " + right.toString());
                    }
                }
                //System.out.println("Passed: " + left.toString() + "(" + right.toString() + ")");
            }
        }
        return true;
    }
    
    private void SetupOrderedCards(Vector<Card> cards)
    {
        cards.clear();
        
        // Default clubs
        Card.Suite suite = Card.Suite.CLUBS;
        
        cards.add(new Card(Card.Value.TWO, suite));
        cards.add(new Card(Card.Value.THREE, suite));
        cards.add(new Card(Card.Value.FOUR, suite));
        cards.add(new Card(Card.Value.FIVE, suite));
        cards.add(new Card(Card.Value.SIX, suite));
        cards.add(new Card(Card.Value.SEVEN, suite));
        cards.add(new Card(Card.Value.EIGHT, suite));
        cards.add(new Card(Card.Value.NINE, suite));
        cards.add(new Card(Card.Value.TEN, suite));
        cards.add(new Card(Card.Value.JACK, suite));
        cards.add(new Card(Card.Value.QUEEN, suite));
        cards.add(new Card(Card.Value.KING, suite));
        cards.add(new Card(Card.Value.ACE, suite));
    }
    
}
